# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import yf12306_class,db_pymsql
import json
import time
from django.shortcuts import render,redirect
from models import Order
from station_names import station_names
# Create your views here.
city={}
city2={}
data=[]
station_names=station_names.decode('utf-8')
#print(type(station_names))
for i in station_names.split('@'):
    if not i:
        continue
    tmp_list=i.split('|')
    city[tmp_list[1]]=tmp_list[2]
    city2[tmp_list[2]]=tmp_list[1]

def search(request):#获取车辆信息
    if request.method =="GET":
        return render(request, "search.html",{'train_date':time.strftime("%Y-%m-%d")})
    else:
        train_date=request.POST.get("train_date")
        train_start = request.POST.get("dp")
        train_arrive = request.POST.get("ar")
        seat = request.POST.getlist("seat")
        train= yf12306_class.Train()
        html = train.getTrain2(train_date, city[train_start], city[train_arrive])
        while html==False:
            html = train.getTrain2(train_date, city[train_start], city[train_arrive])
            time.sleep(3)
        return render(request,"search.html",{"train":html,"train_start":train_start,"train_arrive":train_arrive,"train_date":train_date})
def logins(request):
    if request.method =="GET":
        seat=request.GET.getlist('seat')
        train_no=request.GET.get('train_no')
        train_date=request.GET.get('train_date')
        start_station=request.GET.get('start_station')
        arrive_station=request.GET.get('arrive_station')
        start_time=request.GET.get('start_time')
        arrive_time=request.GET.get('arrive_time')
        #print seat,train_no
        return render(request,"login.html")
    else:
        username = request.POST.get("username")
        password = request.POST.get("password")
        seat=request.GET.get('seat')
        train_no=request.GET.get('train_no')
        train_date=request.GET.get('train_date')
        start_station=request.GET.get('start_station')
        arrive_station=request.GET.get('arrive_station')
        start_time=request.GET.get('start_time')
        arrive_time=request.GET.get('arrive_time')
        if username and password:
            train= yf12306_class.Train()
            print 'train id:',id(train)
            train.username=username
            train.password=password
            login_user=train.code2()
            list2=train.getPassInfo()[2]
            return render(request,"passenger.html",{'list2':list2,'username':username,'password':password,'seat':seat,'train_no':train_no,'train_date':train_date,'start_station':start_station,'arrive_station':arrive_station,'start_time':start_time,'arrive_time':arrive_time,'login_user':login_user})

        return render(request,"login.html")
        #return render(request, "login.html",{'username':username} )
def makeorder(request):
    if request.method=="GET":
        select=request.GET.getlist('select')
        username=request.GET.get('username')
        password=request.GET.get('password')
        seat=request.GET.get('seat')
        train_no=request.GET.get('train_no')
        train_date=request.GET.get('train_date')
        print (train_date)
        start_station=request.GET.get('start_station')
        arrive_station=request.GET.get('arrive_station')
        start_time=request.GET.get('start_time')
        arrive_time=request.GET.get('arrive_time')
        login_user=request.GET.get('login_user')

        if select:
            p=Order()
            for i in select:
                print (i)
                p.passenger+=i+','
            p.passenger=p.passenger[:-1]
            p.order_id=time.strftime('%Y%m%d%H%M%S')
            p.username=username
            p.login_user=login_user
            p.password=password
            p.train_date=train_date
            p.train_no=train_no
            p.seat=seat
            p.start_time=start_time
            p.arrive_time=arrive_time
            p.start_station=start_station
            p.arrive_station=arrive_station
            p.state=0
            p.update_time=time.strftime('%Y-%m-%d %H:%M:%S')
            p.save()
        orderlist=Order.objects.filter().all()
        #print orderlist[0]
        select_passg= request.GET.getlist('select')
        ','.join(select_passg)
        return render(request, "order.html",{'data':orderlist} )

def getPassenger(request):
        train=yf12306_class.Train()
        train.initDc()

def index(request):
    return redirect('12306/search/')

def go(request):
    from django.shortcuts import HttpResponse
    import subprocess,os
    os.chdir(r'g:/git/web/my12306/train')
    print os.path
    sb=subprocess.Popen('python yf12306.py')
    print sb.pid
    return HttpResponse('Start new subprocess')

def sql(request):
    if request.method=="POST":
        sql=request.POST.get('sql')
        print sql
        dbs=db_pymsql.db_mysql()
        data=dbs.exe(sql,1)
        return render(request,'sql.html',{'title':dbs.title,'data':data,'sql':sql})
    else:
        return render(request,'sql.html')
